package com.leo.voice;

import java.util.ArrayList;
import java.util.List;

import android.app.Application;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.util.Log;

public class VoiceApplication extends Application {
	private static String TAG="VoiceApplication";
	private static VoiceApplication mContext;
	private PackageManager  mPackageManager;
	private List<ApplicationInfo> mAppInfoList;
	private List<ApplicationInfo> mThirdpartyAppInfoList;
	private List<ApplicationInfo> mSystemAppInfoList;
	private List<ApplicationInfo> mSDcardAppInfoList;
	private List<ResolveInfo>  mLauncherAppList;
	
	public void onCreate(){
		super.onCreate();
		createHandler();
		generateApplicantionInfo();
	}
	
	public static VoiceApplication getVoiceApplication(){
		return mContext;
	}
	
	void createHandler(){
		mContext = this;
		mPackageManager = this.getPackageManager();
		mThirdpartyAppInfoList = new ArrayList<ApplicationInfo>();
		mSystemAppInfoList = new ArrayList<ApplicationInfo>();
		mSDcardAppInfoList = new ArrayList<ApplicationInfo>();
	}
	
	void generateApplicantionInfo(){
		mAppInfoList = mPackageManager.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
		
		mSystemAppInfoList.clear();
		mThirdpartyAppInfoList.clear();
		mSDcardAppInfoList.clear();
		
		for( ApplicationInfo info : mAppInfoList){
			if( (info.flags & ApplicationInfo.FLAG_SYSTEM) != 0)
				mSystemAppInfoList.add(info);
			
			if( (info.flags&ApplicationInfo.FLAG_SYSTEM) <= 0 )
				mThirdpartyAppInfoList.add(info);
			else if( (info.flags&ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0 )
				mThirdpartyAppInfoList.add(info);
			
			if((info.flags&ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0)
				mSDcardAppInfoList.add(info);
		}
		
		//Make sure we grep all the app that we can launch.
		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_LAUNCHER);
		
		mLauncherAppList = mPackageManager.queryIntentActivities(intent, 0);
	}
	
	public List<ApplicationInfo> getAllInstalledAppInfo(){
		return mAppInfoList;
	}
	
	public List<ApplicationInfo> getThirdPartyInstalledAppInfo(){
		return mThirdpartyAppInfoList;
	}
	
	public List<ApplicationInfo> getSDcardInstalledAppInfo(){
		return mSDcardAppInfoList;
	}
	
	public List<ResolveInfo> getLaunchedAppInfo(){
		return mLauncherAppList;
	}
}
